<template>
  <el-container class="main_con">
    <div class="filter_con ds_block">
      <div class="filter_area">
        <el-input size="medium" v-model="params.name" class="serach_input mr_15 mt_15" placeholder="活动名称"></el-input>

        <date-picker size="medium" @change="gettime" class="mr_15 mt_15"></date-picker>
        <el-button size="medium" @click="search">搜索</el-button>
      </div>
    </div>
    <div class="list_con">
      <el-table @selection-change="SelectHadnle" :data="list" element-loading-text fit highlight-current-row class="main-loading">
        <el-table-column prop="agent" label="分销商">
          <template slot-scope="scope">
            <span>{{ scope.row.agent.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="level_type" label="分销商等级">
          <template slot-scope="scope">
            <span>{{ scope.row.agentGrade.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="考核名称"></el-table-column>
        <el-table-column prop="level_type" label="降级前等级">
          <template slot-scope="scope">
            <span>{{ scope.row.agentOldGrade.name }}</span>
          </template>
        </el-table-column>

        <el-table-column prop="create_time" label="降级时间"></el-table-column>
      </el-table>
      <div class="table_footer">
        <!--  <el-button size="medium">批量删除
        </el-button>-->
        <el-pagination
          @current-change="ChangeCurrent"
          class="pages"
          background
          layout="prev, pager, next"
          :total="total"
          :page-size="size"
          :page-count="page"
          hide-on-single-page
        ></el-pagination>
      </div>
    </div>
  </el-container>
</template>

<script>
import {mapGetters} from 'vuex';
import {getassessmentdegrade, activitystatus, delactivity, activitysend} from '@/api/mall/commission';
import DatePicker from '@/components/DatePicker/index';
import {fitter_keyword} from '@/utils/index';
import {agent_gradelist} from '@/api/mall/agent';

export default {
  data() {
    return {
      params: {
        name: '',
        mobile: '',
        start_time: '',
        end_time: ''
      },
      paramsdf: {},
      page: 1,
      size: 10,
      total: 0,
      listLoading: true,
      list: [],
      drawer: false,
      direction: 'rtl',
      user_id: '',
      user_type: 'add',
      selectionData: [],
      url: '',
      gradelist: [],
      statuslist: [
        {
          value: 0,
          label: '未开始'
        },
        {
          value: 1,
          label: '开启'
        },
        {
          value: 2,
          label: '暂停'
        },
        {
          value: 3,
          label: '结束'
        },
        {
          value: 4,
          label: '关闭'
        }
      ]
    };
  },
  components: {
    DatePicker
  },
  computed: {
    ...mapGetters(['allroleslist'])
  },
  mounted() {
    this.agent_gradelist();
  },
  filters: {
    statustxt(status) {
      switch (status) {
        case 0:
          return '未开始';
          break;
        case 1:
          return '进行中';
          break;
        case 2:
          return '停止';
          break;
        case 3:
          return '结束';
          break;
        case 4:
          return '关闭';
          break;
      }
    }
  },
  methods: {
    agent_gradelist() {
      let data = {
        page: 1,
        size: 10
      };
      agent_gradelist(data).then(res => {
        let list = res.msg;
        list.unshift({id: 0, name: '默认等级'});
        this.gradelist = list;
        this.getlist();
      });
    },

    doCopy(data) {
      this.$copyText(data).then(
        res => {
          //this.$alert('复制成功');
          this.$message({
            message: '复制成功',
            type: 'success',
            duration: 2000
          });
          // data.visible = false;
        },
        error => {
          this.$alert('无法复制');
        }
      );
    },
    search() {
      this.paramsdf = fitter_keyword(this.params);
      this.getlist();
    },
    //启用，禁用
    EnableHandle(data, status) {
      let text = '';
      switch (status) {
        case 1:
          text = '确认继续该活动？';
          break;
        case 2:
          text = '确认暂停该活动？';
          break;
        case 3:
          text = '确认关闭该活动';
          break;
      }
      let _this = this;
      this.$confirm(text)
        .then(_ => {
          let params = {id: data.id, status: status};
          activitystatus(params).then(res => {
            _this.getlist();
          });
        })
        .catch(_ => {});
    },
    //选择
    SelectHadnle(selection) {
      this.selectionData = selection;
    },
    //删除
    handleClose(id) {
      let _this = this;
      this.$confirm('确认删除？')
        .then(_ => {
          let data = {
            id: id
          };
          delactivity(data).then(res => {
            _this.getlist();
          });
        })
        .catch(_ => {});
    },
    SendHandle(id) {
      let _this = this;
      this.$confirm('确认发送奖励？')
        .then(_ => {
          let data = {
            id: id
          };
          activitysend(data).then(res => {
            _this.getlist();
          });
        })
        .catch(_ => {});
    },
    //新增
    addHandle() {
      this.$router.push('/assessment/item');
    },
    //编辑
    EditHandle(id) {
      this.$router.push({path: '/assessment/item', query: {id: id}});
    },
    ReadHandle(id) {
      this.$router.push({path: '/assessment/award', query: {id: id}});
    },
    //编辑完成
    beClose() {
      (this.drawer = false), this.getlist();
    },
    //获取列表
    getlist() {
      this.listLoading = true;
      let data = {
        page: this.page,
        size: this.size
      };
      getassessmentdegrade(data).then(res => {
        var jsondata = res.msg;
        let list = jsondata.data;
        list.forEach(a => {
          if (a.level_type == 1) {
            let levels = JSON.parse(a.levels);

            a.levelslist = this.gradelist.filter(_ => levels.indexOf(_.id) > -1);
          }
        });

        this.list = list;

        this.total = jsondata.total;
        // this.list = response.data.items
        this.listLoading = false;
      });
    },
    //获取时间
    gettime(value) {
      this.params.start_time = value[0];
      this.params.end_time = value[1];
    },
    //切换页码
    ChangeCurrent(page) {
      this.page = page;
      this.getlist();
    }
  }
};
</script>

<style lang="scss" scoped>
.login_url {
  padding: 8px 16px;
  background-color: #f4f4f5;
  color: #909399;
  box-sizing: border-box;
  border-radius: 4px;
  display: flex;
  align-items: center;
  font-size: 14px;
  p {
    margin: 0;
    margin-right: 10px;
  }
  .el-button {
    padding: 0;
  }
}
</style>
